<?php
require_once("page.class.php");
require_once("adrese.class.php");
require_once("rokovnik.report.class.php");

class rokovnik extends page{
	
	var $db;
	var $adresa;
	
	function rokovnik($get,$post){
		
		$this->db = new db_functions();
		$this->adresa = new adrese();
		$this->loadConfig();
		
		$action = getFromInput("a");
		$mod = getFromInput("mod");
		switch ($action){
			case "distrib":
				switch ($mod){
					case "teritory":
						$this->distribTeritory($_POST);
						break;
					case "newTeritory":
						$this->newTeritory($_POST);
						break;
					case "saveTeritory":
						$tmp = explode(".",$post["datum"]);
						$datum = $tmp[2]."-".$tmp[1]."-".$tmp[0];
						$post["desc"] = utf8RawUrlDecode($post["desc"]);
						$post["desc"] = utf8RawChrDecode($post["desc"]);
						
						$q = "UPDATE rokovnik_teritorije SET "
							."code='".$post["name"]."',"
							."`desc`='".$post["desc"]."',"
							."date='$datum' WHERE id=".$post["id"];
						//echo "$q\n";
						if($this->db->alterTable($q)){
							echo "Izmene sačuvane.";
						}else{
							echo mysql_error();
						}
						break;
					case "delTeritory":
						if($this->db->alterTable("DELETE FROM rokovnik_teritorije WHERE id=".$_GET["id"])){
							echo "Teritorija obrisana.";
						}else{
							echo mysql_error();
						}
						break;
				}
				break;
			case "kom":
				switch ($mod){
					case "contact":
						$this->komercijalistaContacts();
						break;
					case "contactAdd":
						$this->komercijalistaContactSave();
						break;
					case "bykomitent":
						$this->komercijalistaContanctByKomitent();
						break;
					case "del":
						$this->komercijalistaContactDelete();
						break;
					case "deactAll":
						$this->komercijalistaDeactivateAll();
						break;
					case "actDeact":
						$this->komercijalistaActDeact();
						break;
					default:
						$this->komercijalista();
						break;
				}
				break;
			case "komSave":
				$this->komercijalistaSave($post);
				break;
			case "findKom":
				$this->searchKomitent(readValFromInput("k"),$get["man"],$get["level"]);
				break;
			case "komDetails":
				$this->komDetails($get["id"]);
				break;
			case "yapyap":
				$this->YapYap();
				break;
			case "komeDetailsDelAddresses":
				$this->komeDetailsDelAddresses($get["id"]);
				break;
			case "komHistory":
				$this->callHistory($get["kom"]);
				break;
			case "manager":
				switch ($mod){
					case 'komercijalaTask':
						$this->managerKomercijalaTask();
						break;
					default:
						$this->manager();
						break;
				}
				break;
			case "searchSastanak":
				$this->searchSastanak($get);
				break;
			case "addMeetingFeedback":
				$this->addMeetingFeedback($get);
				break;
			case "addNew":
				$this->addNew($post);
				break;
			case "delOne":
				$this->delOne();
				break;
			case "report":
				$report = new rokovnik_report($this,$get,$post);
				break;
			case "adminCalls":
				switch ($mod){
					case "del":
						$this->delCall($get["id"]);
						break;
					case "edit":
						$this->editCall($get["id"]);
						break;
					case "save":
						$this->saveEditCall($post);
						break;
					default:
						$this->adminCalls($get);
				}
				break;
			case "adminMeetings":
				switch ($mod){
					case "showEdit":
						$this->showEdit($_GET["id"]);
						break;
					case "delete":
						$this->deleteMeeting($get["id"]);
						break;
					case "browseMeetingTickets":
						$this->browseMeetingTickets();
						break;
					case "generateMeetingTickets":
						$this->generateMeetingTickets();
						break;
					case "exportMeetingTickets":
						$this->exportMettingTickets();
						break;
					case "newMeetingTicket":
						$this->newMeetingTicket();
						break;
					case "updateMeetingTicketKomercijalista":
						$this->updateMTKomercijalista();
						break;
					case "saveTicket":
						$this->saveMeetingTicket();
						break;
					case "deleteTicket":
						$this->deleteMeetingTicket();
						break;
					case "editTicket":
						$this->editMeetingTicket();
						break;
					default:
						$this->browseMeetings($post);
						break;
				}
				break;
			case "doTheAlarm":
				switch ($mod){
					case "manager":
						$this->updateAlarmsManager();
						break;
					default:
						$this->updateAlarms();
						break;
				}
				break;
			default:
				$this->displayNew();
				break;
		}
	}
	function newTeritory($post){
		$post["desc"] = utf8RawUrlDecode($post["desc"]);
		$post["desc"] = utf8RawChrDecode($post["desc"]);
		$tmp = explode(".",$post["datum"]);
		$post["datum"] = $tmp[2]."-".$tmp[1]."-".$tmp[0];
		$q = "INSERT INTO rokovnik_teritorije VALUES('','".$post["name"]."','".$post["desc"]."','".$post["datum"]."')";
		if($this->db->alterTable($q)){
			echo "Unos izvršen.";
		}else{
			echo mysql_error();
		}
	}
	function distribTeritory($post){
		$q = "SELECT id, code, `desc` FROM rokovnik_teritorije WHERE ";
		if(isset($post["datum"])){
			$tmp = explode(".",$post["datum"]);
			$date_q = $tmp[2]."-".$tmp[1]."-".$tmp[0];
			$q .= "date='$date_q' ";
			$this->assign("datum",$post["datum"]);
		}else{
			$q .= "date='".date("Y-m-d")."' ";
			$this->assign("datum",date("d.m.Y"));
		}
		$q .= "ORDER BY code";
		
		$res = $this->db->select2($q);
		
		if($res[0]){
			foreach ($res as $row){
				$ids[] = $row["id"];
				$names[] = $row["code"];
				$descs[] = nl2br($row["desc"]);
			}
			$this->assign("id",$ids);
			$this->assign("name",$names);
			$this->assign("desc",$descs);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
			$this->assign("message","Nema teritorija za zadati datum");
		}
		
		echo $this->parseTemplate("rokovnik/teritorije.tpl");
	}
	
	function komercijalista(){

		$polja = $this->get_Radnici();
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
		$polja = $this->get_RokovnikPoreklo();
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo_name",$polja["polje_ime"]);
		$polja = $this->get_RokovnikOdgovor();
			$this->assign("odgovor_id",$polja["polje_id"]);
			$this->assign("odgovor_name",$polja["polje_ime"]);
		
		$ter = $this->get_RokovnikTeritorije();
			//print_rarray($ter);
			
			$options = "\n";
			foreach ($ter["polje_id"] as $k=>$id){
				$name = $ter["polje_code"][$k];
				$date = $ter["polje_date"][$k];
				$desc = str_replace("\n",", ",$ter["polje_desc"][$k]);
				$tmp = explode("-",$date,3);
				$date_display = $tmp[2].".".$tmp[1].".".$tmp[0];
				
				$sele = $ter["polje_selected"][$k];
				
				if($date != @$ter["polje_date"][$k-1]){
					$options .= '<optgroup label="'.$date_display.'" date="'.$date_display.'">';
				}
				
				$options .= "\n".'<option title="'.$desc.'" value="'.$id.'" date="'.$date_display.'"';
				
				if(strlen($sele)){
					$options .= " selected";
				}
				
				$options .= '>'.$name.'</option>';
				
				if($date != @$ter["polje_date"][$k+1]){
					$options .= "\n".'</optgroup>';
				}
			}
			$this->assign("teritorije_select",$options);	
		/*$polja = $this->get_RokovnikTeritorije();
			$this->assign("teritorija_id",$polja["polje_id"]);
			$this->assign("teritorija_name",$polja["polje_code"]);
			$this->assign("teritorija_desc",$polja["polje_desc"]);
			$this->assign("teritorija_datum",$polja["polje_date"]);*/
		
		echo $this->parseTemplate("rokovnik/komercijalista.tpl");
	}
	function komercijalistaSave($data){
		foreach ($data as $k=>$v){
			$v = utf8RawUrlDecode($v);
			$data[$k] = utf8RawChrDecode($v);
		}
		
		$now = time();
		
		$tmp = explode(".",$data["visit_date"]);
		$visit = makeUnixTime($data["visit_hour"],$data["visit_minute"],0,$tmp[1],$tmp[0],$tmp[2]);
		
		$tmp = explode(".",$data["zak_call_date"]);
		$zak_call = makeUnixTime($data["zak_call_hour"],$data["zak_call_minute"],0,$tmp[1],$tmp[0],$tmp[2]);
		
		$q = "INSERT INTO rokovnik_komercijala VALUES('',"
			.$_COOKIE["ypu"].","
			."'".$data["komitent_id"]."',"
			."'".$data["radnik"]."',"
			."'".$data["teritorija"]."',"
			."'".$visit."',"
			."'".$now."',"
			."'".$zak_call."',"
			."'".$data["poreklo"]."',"
			."'".$data["odgovor"]."',"
			."'".$data["napomena"]."')";
		//echo $q."\n\n";print_rarray($data);
		if($this->db->alterTable($q)){
			echo "Unos izvršen.";
		}else{
			echo mysql_error();
		}
	}
	function komercijalistaContacts(){
		$selectSearchTerms = "<< Odaberite neki kriterijum pretrage";
		
		$radnici = $this->get_Radnici();
			$this->assign("radnik_id",$radnici["polje_id"]);
			$this->assign("radnik_ime",$radnici["polje_ime"]);
			$this->assign("radnik_prezime",$radnici["polje_prezime"]);
		
		$mesta = $this->get_Lokacije(1);
		$lokops = $this->get_Lokacije(2);
		$nas = $this->get_Lokacije(3);
		
		$mesta_type = array_fill(0,count($mesta["polje_id"]),"1");
		$lokops_type = array_fill(0,count($lokops["polje_id"]),"2");
		$nas_type = array_fill(0,count($nas["polje_id"]),"3");
		
		
		
		$lokacije["polje_id"] = array_merge(array_merge($mesta["polje_id"],$lokops["polje_id"]),$nas["polje_id"]);
		$lokacije["polje_ime"] = array_merge(array_merge($mesta["polje_ime"],$lokops["polje_ime"]),$nas["polje_ime"]);
		$lokacije["polje_tip"] = array_merge(array_merge($mesta_type,$lokops_type),$nas_type);
		$this->assign("lid",$lokacije["polje_id"]);
		$this->assign("lok_name",$lokacije["polje_ime"]);
		$this->assign("lok_tip",$lokacije["polje_tip"]);
		
		$filter = getFromInput("filter");
		if ($filter) {
			$filter_komer = readValFromInput("filter_komercijalista");
			$filter_komit = readValFromInput("filter_komitent");
			$filter_lokac = readValFromInput("filter_lokacija");
			
			$komers = $koms = $loks = array();
			if (strlen($filter_komer) || strlen($filter_komit) || strlen($filter_lokac)) {
				$this->assign("filter_komercijalista",$filter_komer);
				$this->assign("filter_komitent",$filter_komit);
				$this->assign("filter_lokacija",$filter_lokac);
				// get radnici
				if (strlen($filter_komer)) {
					$res = $this->db->select1("SELECT radnik_id FROM radnici WHERE CONCAT(radnik_ime,' ', radnik_prezime) LIKE '%".addslashes($filter_komer)."%'");
					foreach ($res as $k=>$row){
						$komers[$k] = $row[0];
					}
				}
				// get komitenti
				if (strlen($filter_komit)) {
					$res = $this->db->select1("SELECT komitent_id FROM komitenti WHERE komitent_id='".addslashes($filter_komit)."' OR komitent_name LIKE '%$filter_komit%' OR komitent_name1 LIKE '%$filter_komit%'");
					foreach ($res as $k=>$row){
						$koms[$k] = $row[0];
					}
				}
				// get lokacije
				if (strlen($filter_lokac)) {
					$res = $this->db->select1("SELECT lid FROM lokacije WHERE lok_naziv LIKE '%".addslashes($filter_lokac)."%'");
					foreach ($res as $k=>$row){
						$loks[$k] = $row[0];
					}
				}
				
				$q = "SELECT * FROM komercijala_kontakti WHERE 1";
				if (count($komers) OR count($koms) OR count($loks)) {
					if (count($komers)) {
						$q .= " AND (0";
						foreach ($komers as $v){
							$q .= " OR radnik=$v";
						}
						$q .= ")";
					}
					if (count($koms)) {
						$q .= " AND (0";
						foreach ($koms as $v){
							$q .= " OR komitent=$v";
						}
						$q .= ")";
					}
					if (count($loks)) {
						$q .= " AND (0";
						foreach ($loks as $v){
							$q .= " OR lokacija=$v";
						}
						$q .= ")";
					}
					
					$res = $this->db->select2($q);
					if ($res !== false) {
						foreach ($res as $k=>$row){
							$_id[$k]			= $row["id"];
							$_komercijalista[$k]= $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$row["radnik"]." LIMIT 1");
							$_komitent[$k]		= $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$row["komitent"]." LIMIT 1");
							$_lokacija[$k]		= $this->db->getOne("SELECT lok_naziv FROM lokacije WHERE lid=".$row["lokacija"]." LIMIT 1");
							$_time_in[$k]		= int2date($row["time_unos"]);
							$_time_ishod[$k]	= int2date($row["time_ishod"]);
							$_active[$k]		= $row["active"]>0?"1":"0";
						}
						$this->assign("id",$_id);
						$this->assign("komercijalista",$_komercijalista);
						$this->assign("komitent",$_komitent);
						$this->assign("lokacija",$_lokacija);
						$this->assign("vreme_unosa",$_time_in);
						$this->assign("vreme_ishoda",$_time_ishod);
						$this->assign("active",$_active);
					}else{
						$this->assign("comment_start","<!--");
						$this->assign("comment_end","-->Nema rezultata");
					}
					
				}else{
					$this->assign("comment_start","<!--");
					$this->assign("comment_end","-->Nema rezultata");
				}
				
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->$selectSearchTerms");
			}
			
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->$selectSearchTerms");
		}
		
		echo $this->parseTemplate("rokovnik/komercijalista_contacts.tpl");
	}
	function komercijalistaContactSave(){
		$komerc = trim(getFromInput("add_komercijalista"));
		$komite = trim(getFromInput("add_komitent_id"));
		$lokaci = trim(getFromInput("add_lokacija"));
		
		$datum = trim(getFromInput("add_date"));
		$datum = date2int($datum);
		
		if (!strlen($komerc) || !strlen($komite) || !strlen($lokaci) || !$datum) {
			die("Morate popuniti sva polja!");
		}
		
		$userid = $this->getUserId();
		$now = time();
		
		$ima = $this->db->getOne("SELECT 1 FROM komercijala_kontakti WHERE komitent=$komite AND radnik=$komerc LIMIT 1");
		if($ima){
			die("Kontakt ".$this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$komerc LIMIT 1")." -> ".$this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=$komite LIMIT 1")." je već zabeležen!");
		}
		
		$q = "INSERT INTO komercijala_kontakti VALUES('',"
			."$userid,"
			."$komerc,"
			."$komite,"
			."$lokaci,"
			."$now,"
			."$datum,"
			."1,"
			."''"
			.")";
		if ($this->db->alterTable($q)) {
			echo "Kontakt je sačuvan";
		}else{
			echo mysql_error();
		}
		//echo "$komerc\n$komite\n$lokaci\n$datum";
	}
	function komercijalistaContactDelete(){
		$id = getFromInput("id");
		if ($this->db->alterTable("DELETE FROM komercijala_kontakti WHERE id=$id LIMIT 1")) {
			echo "Kontakt je obrisan";
		}else{
			echo mysql_error();
		}
	}
	function komercijalistaDeactivateAll(){
		$komerid = getFromInput("id");
		$komername = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$komerid LIMIT 1");
		
		$q = "UPDATE komercijala_kontakti SET active=0 WHERE radnik=$komerid";
		if ($this->db->alterTable($q)) {
			echo "Svi kontakti radnika $komername su deaktivirani.";
		}else{
			echo mysql_error();
		}
	}
	function komercijalistaActDeact(){
		$act = getFromInput("act");
		$id = getFromInput("id");
		$act1 = $act>0?0:1;
		$msg = $act>0?"deaktiviran":"aktiviran";
		if ($this->db->alterTable("UPDATE komercijala_kontakti SET active=$act1 WHERE id=$id LIMIT 1")) {
			echo "Kontakt je $msg.";
		}else{
			echo mysql_error();
		}
	}
	function komercijalistaContanctByKomitent(){
		$komid = getFromInput("id");
		$q = "SELECT radnik_ime, radnik_prezime, lokacija, time_ishod "
			."FROM komercijala_kontakti kk, radnici r "
			."WHERE kk.radnik=r.radnik_id "
			."AND komitent=$komid AND active=1 "
			."ORDER BY time_ishod DESC";
		$contacts = $this->db->select2($q);
		if ($contacts!==false) {
			$code=1;
			$ret = "<div class=\"area\"><ol>";
			foreach ($contacts as $row){
				$ret .= "<li><strong>".$row["radnik_ime"]." ".$row["radnik_prezime"]."</strong><br />".$this->db->getOne("SELECT lok_naziv FROM lokacije WHERE lid=".$row["lokacija"]." LIMIT 1").", ".int2date($row["time_ishod"])."</li>";
			}
			$ret .= "</ol></div>";
		}else{
			$code=0;
			$ret = "";
		}
		echo "$code{!}$ret";
	}
	
	function manager(){
		$polja = $this->get_RokovnikFeedbacks();
			$this->assign("fb_id",$polja["polje_id"]);
			$this->assign("fb_desc",$polja["polje_ime"]);
			
		$radnici = $this->db->select2("SELECT radnik_id, radnik_ime, radnik_prezime FROM radnici WHERE radnik_akt=1 ORDER BY radnik_ime, radnik_prezime");
		foreach ($radnici as $radnik){
			$ids[] = $radnik["radnik_id"];
			$ime[] = $radnik["radnik_ime"];
			$pre[] = $radnik["radnik_prezime"];
		}
		$this->assign("radnik_id",$ids);
		$this->assign("radnik_ime",$ime);
		$this->assign("radnik_prezime",$pre);
		echo $this->parseTemplate("rokovnik/manager.tpl");
	}
	function managerKomercijalaTask(){
		$len = getFromInput("length");
		for ($i=0; $i<$len; $i++){
			$data[$i]["id"] = readValFromInput("komid_$i");
			$data[$i]["name"] = readValFromInput("komname_$i");
			$data[$i]["adresa"] = preg_replace('/<br\s*\/*>/',', ',readValFromInput("komaddress_$i"));
			$data[$i]["dels"] = preg_replace('/<br\s*\/*>/',', ',readValFromInput("komdels_$i"));
		}
		
		foreach ($data as $k=>$kom){
			$komid[$k] = $kom["id"];
			$komname[$k] = $kom["name"];
			$komadresa[$k] = $kom["adresa"];
			$komdels[$k] = $kom["dels"];
			
			$wrongadr = $this->db->getOne("SELECT komitent_view_status FROM komitenti WHERE komitent_id=".$kom["id"]);
			$komkom[$k] = $wrongadr;
			if ($wrongadr==4) {
				$komkom[$k] = "netačni podaci";
			}else{
				$q = "SELECT rk.obavljen, r.kontakt "
					."FROM rokovnik r, rokovnik_kontakt rk "
					."WHERE r.kontakt=rk.id "
					."AND komitent_id=".$kom["id"]." "
					."ORDER BY time DESC "
					."LIMIT 0,3";
				$neobavljen = $this->db->select1($q);
				if ($neobavljen!==false) {
					if (count($neobavljen)>2) {
						$obavljen=false;
						foreach ($neobavljen as $n){
							if ($n[0]>0) {
								$obavljen=true;
								break;
							}
						}
					}else{
						$obavljen=true;
						foreach ($neobavljen as $n){
							if ($n[1]==7 || $n[1]==10) {
								$obavljen=false;
								break;
							}
						}
					}
				}
				$nn = $obavljen?"":"netačni podaci";
				$komkom[$k] = "$nn&nbsp;";
			}
			
		}
		
		/*$db = $this->db->alterTable("CREATE TEMPORARY TABLE mytable(id varchar(3), val varchar(500))");
		//$res = $this->db->select2("DESCRIBE mytable");
		//print_rarray($res);
		foreach ($komadresa as $k=>$v){
			$this->db->alterTable("INSERT INTO mytable VALUES('$k','".addslashes($v)."')");
		}
		$res = $this->db->select2("SELECT * FROM mytable ORDER BY val");
		foreach ($res as $row){
			$_komadresa[$row["id"]] = $row["val"];
		}
		//print_rarray($_komadresa);
		$this->db->alterTable("DROP TABLE mytable");*/
		$_komadresa = $komadresa;
		natsort($_komadresa);
		$i=0;
		foreach ($_komadresa as $k=>$v){
			$sorted["id"][$i] = $komid[$k];
			$sorted["name"][$i] = $komname[$k];
			$sorted["adresa"][$i] = $komadresa[$k];
			$sorted["dels"][$i] = $komdels[$k];
			$sorted["komentar"][$i] = $komkom[$k];
			$i++;
		}
		
		$this->assign("komid",$sorted["id"]);
		$this->assign("komname",$sorted["name"]);
		$this->assign("komadresa",$sorted["adresa"]);
		$this->assign("komdels",$sorted["dels"]);
		$this->assign("komkom",$sorted["komentar"]);
		echo $this->parseTemplate("rokovnik/manager_komercijala_tasklist.tpl");
	}
	function displayNew(){
		$this->assign("user",$this->getUserId());
		$this->loadConfig();
		$alarmInterval = $this->getFromConfig("alarm_interval");
		
		$ef = getFromInput("ef");
		$efid = getFromInput("efid");
		$efname = getFromInput("efname");
		
		if(strlen($ef)>0 && $efid>0 && strlen($efname)>0){
			$evalfunc = "$ef('$efid','".addslashes($efname)."',0,'0');";
			$this->assign("forEVAL",$evalfunc);
		}
		
		
		$this->assign("alarm_interval",$alarmInterval);
		
		
		//$this->assign("forEVAL","alert('Cao')");
		
		$polja = $this->get_RokovnikPoreklo();
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo_name",$polja["polje_ime"]);
		$polja = $this->get_RokovnikKontakt();
			$this->assign("kontakt_id",$polja["polje_id"]);
			$this->assign("kontakt_name",$polja["polje_ime"]);
			$this->assign("kontakt_obavljen",$polja["polje_obavljen"]);
		$polja = $this->get_RokovnikOdgovor();
			$this->assign("odgovor_id",$polja["polje_id"]);
			$this->assign("odgovor_name",$polja["polje_ime"]);
			$this->assign("odgovor_ovlascen",$polja["polje_ovlascen"]);
		$polja = $this->get_RokovnikResultTypes();
			$this->assign("result_type_id",$polja["polje_id"]);
			$this->assign("result_type_name",$polja["polje_ime"]);
		
		
		echo $this->parseTemplate("rokovnik/telemarketing.tpl");
	}
	function addNew($post){
		
		foreach ($post as $k=>$v){
			$v = utf8RawUrlDecode($v);
			$post[$k] = utf8RawChrDecode($v);
		}
		//print_r($post);
		$user = $post["user"];
		$date = time();
		$komi = $post["komitent_id"];
		$pore = $post["poreklo"];
		$kont = $post["kontakt"];
		$odgo = $post["odgovor"];
		$spdo = $post["spec_dogadjaj"];
		$napo = $post["napomena"];
		
		$resu = $post["result"];
		$rdesc = $post["result_desc"];
		$rdat = explode(".",$post["result_date"]);
		$rhou = $post["result_hour"];
		$rmin = $post["result_minute"];
		$rtime = makeUnixTime($rhou,$rmin,0,$rdat[1],$rdat[0],$rdat[2]);
		
		$q = "INSERT INTO rokovnik VALUES("
			."'',$komi,$user,$date,$pore,$kont,$odgo,'$spdo','$napo'"
			.")";
		if(!$this->db->alterTable($q)){
			die(mysql_error());
		}
		$rok_id = $this->db->getOne("SELECT LAST_INSERT_ID() FROM rokovnik");
		$q = "INSERT INTO rokovnik_result VALUES('',$rok_id,$rtime,'$rdesc',$resu)";
		if(!$this->db->alterTable($q)){
			die(mysql_error());
		}
		echo "Unos izvršen";
	}
	function delOne(){
		$id = getFromInput("id");
		$code=1;
		if ($id>0) {
			$q = "SELECT 1 FROM rokovnik r, rokovnik_result rr "
				."WHERE r.id=rr.rokovnik_id "
				."AND rr.type=3 "
				."AND r.id=$id "
				."LIMIT 1";
			$ima = $this->db->getOne($q);
			if (!$ima) {
				$q = "DELETE rokovnik.*, rokovnik_result.* FROM rokovnik, rokovnik_result WHERE rokovnik.id=rokovnik_result.rokovnik_id AND rokovnik.id=$id";
				if ($this->db->alterTable($q)) {
					$code=0;
					$msg = "Poziv je obrisan.";
				}else{
					$msg = mysql_error();
				}
			}else{
				$msg = "Poziv ne može biti obrisan zato što je ishod poziva sastanak.";
			}
		}
		echo "$code{!}$msg";
	}
	function searchKomitent($kword,$manager=0,$level=0){
		$q = "SELECT DISTINCT komitent_id, komitent_name, komitent_adresa "
			."FROM komitenti ";
		if(preg_match('/^\d+$/',$kword)){
			$q .= "WHERE komitent_id='".addslashes($kword)."' ";
		}else{
			$q .= "WHERE komitent_name LIKE '%".addslashes($kword)."%' ";
		}
		$q .= "ORDER BY komitent_name";
		$res = $this->db->select2($q);
		
		if($res[0]){
			if(count($res) > MAX_SEARCH_LIST){
				$res = array_splice($res,0,MAX_SEARCH_LIST);
				$this->assign("search_info","Pronađeno ".count($res)." komitenata. Prikazujem prvih ".MAX_SEARCH_LIST.".<br />Ako niste dobili željeni rezultat, unesite opširniju reč pretrage.");
			}else{
				$this->assign("search_info","Pronađeno ".count($res)." komitenata.");
			}
			foreach ($res as $k=>$row){
				$listnum[] = $row["komitent_id"];
				
				$tels = $this->db->select1("SELECT DISTINCT telefon FROM komitenti_telefoni WHERE kom_id=".$row["komitent_id"]." AND del_id=0 ORDER BY fax LIMIT 2");
				$tel = "";
				if($tels[0]){
					foreach ($tels as $t){
						if(strlen(trim($t[0])))
						$tel .= $t[0].", ";
					}
					$tel = " &middot; ".substr($tel,0,strlen($tel)-2);
				}
				$adr = $this->adresa->getAll($row["komitent_adresa"]);
				$adresa = $adr["lokacija"]["mesto"]["name"];
				$len = strlen($adr["ulica"]);
				if($len){
					$adresa .= ", ".$adr["ulica"]." ".$adr["broj"];
				}
				$small_info = "<br /><span class=\"info\">$adresa".$tel."</span>";
				
				// delatnosti
				$q = "SELECT DISTINCT komdel_adresa_id FROM komitenti_delatnosti WHERE komdel_kom_id=".$row["komitent_id"]." AND komdel_adresa_id !=".$row["komitent_adresa"];
				$r = $this->db->select1($q);
				if($r[0]){
					foreach ($r as $jv){
						$a = $this->adresa->getAll($jv[0]);
						$q = "SELECT telefon FROM komitenti_telefoni WHERE kom_id=".$row["komitent_id"]." AND adresa=".$jv[0]." AND del_id != 0 ORDER BY fax LIMIT 2 ";
						$tels = $this->db->select1($q);
						$tt="";
						if($tels){
							foreach ($tels as $t){
								$tt .= " &middot; ".$t[0];
							}
							$tt = substr($tt,1);
						}
						$aa = $a["lokacija"]["mesto"]["name"];
						if(strlen($a["ulica"])){
							$aa .= ", ".$a["ulica"]." ".$a["broj"];
						}
						$small_info .= "<br /><span class=\"info\">$aa".$tt."</span>";
					}
				}
				
				$listitems[] = "<strong>".$row["komitent_name"]."</strong>$small_info";
				
				
				if($manager){
					if($level){
						$actions[] = "setRokovnikKomitent('".$row["komitent_id"]."','".addslashes(str_replace('"','&quot;',$row["komitent_name"]))."',1,'$level');";
					}else{
						$actions[] = "setRokovnikKomitent('".$row["komitent_id"]."','".addslashes(str_replace('"','&quot;',$row["komitent_name"]))."',1,'$level');closeYpAjaxForm()";
					}
				}else{
					if($level){
						$actions[] = "setRokovnikKomitent('".$row["komitent_id"]."','".addslashes(str_replace('"','&quot;',$row["komitent_name"]))."',0,'$level');";
					}else{
						$actions[] = "setRokovnikKomitent('".$row["komitent_id"]."','".addslashes(str_replace('"','&quot;',$row["komitent_name"]))."',0,'$level');closeYpAjaxForm();";
					}
				}
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
			$this->assign("search_info","Nema rezultata za traženu ključnu reč.");
		}
		//print_r($actions);
		
		$this->assign("search_kword",$kword." ");
		$this->assign("list_num",$listnum);
		$this->assign("list_item",$listitems);
		$this->assign("liaction",$actions);
		
		echo $this->parseTemplate("rokovnik/search_list.tpl");
	}
	function komDetails($id){
		
		/*$checkfox = getFromInput("checkfox");
		//echo $checkfox;
		if ($checkfox==1) {
			$yapfox = $this->db->getOne("SELECT yap FROM yapfox WHERE fox=$id LIMIT 1");
			if ($yapfox !== null) {
				$id = $yapfox;
			}
		}*/
		$this->assign("id",$id);
		
		$q = "SELECT komitent_name, komitent_adresa, komitent_vlasnik_name, komitent_direktor, komitent_direktor_tel, komitent_predst, komitent_predst_tel "
			."FROM komitenti WHERE komitent_id=$id LIMIT 1";
		list($komitent) = $this->db->select2($q);
		//print_rarray($komitent);
		
		$q = "SELECT DISTINCT komdel_del_id, delatnost_ime, komdel_adresa_id "
			."FROM komitenti_delatnosti, delatnosti "
			."WHERE komdel_del_id=delatnost_id "
			."AND komdel_kom_id=$id "
			."ORDER BY delatnost_ime";
		$delatnosti = $this->db->select2($q);
		
		// delatnosti
		if($delatnosti !== false){
			$other_adrese = false;
			$adrese = array();
			$delids = array();
			foreach ($delatnosti as $del){
				$imaadresa = array_search($del["komdel_adresa_id"],$adrese);
				$imadelatnost = array_search($del["komdel_del_id"],$delids);
				if($imadelatnost === false){
					$delids[] = $del["komdel_del_id"];
					$delnames[] = $del["delatnost_ime"];
				}
				if ($del["komdel_adresa_id"] != $komitent["komitent_adresa"]) {
					$other_adrese = true;
				}
			}
			$this->assign("komdel_delids",$delids);
			$this->assign("komdel_delnames",$delnames);
			if (!$other_adrese) {
				$this->assign("comment_start_other_adrese","<!--");
				$this->assign("comment_end_other_adrese","-->");
			}
		}
		
		// adresa
		$adresa = $this->adresa->getAll($komitent["komitent_adresa"]);
		$adr = ""; $ptt = "";
		if(strlen($adresa["ulica"])){
			$ul = $adresa["ulica"]." ".$adresa["broj"];
		}
		
		$adr .= $adresa["lokacija"]["mesto"]["name"];
		if (strlen($adresa["lokacije"]["mesto"]["ptt"])) {
			$ptt = $adresa["lokacije"]["mesto"]["ptt"];
		}
		
		if (isset($adresa["lokacija"]["opstina"])) {
			$adr .= " / ".$adresa["lokacija"]["opstina"]["name"];
			$ptt = $adresa["lokacija"]["opstina"]["ptt"];
		}
		
		if (isset($adresa["lokacija"]["naselje"])) {
			$adr .= " / ".$adresa["lokacija"]["naselje"]["name"];
			$ptt = $adresa["lokacija"]["naselje"]["ptt"];
		}
		
		$this->assign("adresa","$ul<br />$ptt $adr");
		
		// telefoni
		$tels = $this->db->select2("SELECT telefon,fax FROM komitenti_telefoni WHERE del_id=0 AND kom_id=$id");
		if($tels !== false){
			foreach ($tels as $tel){
				if($tel["fax"]){
					$faxes[] = $tel["telefon"];
				}else{
					$teles[] = $tel["telefon"];
				}
			}
			$this->assign("telefoni",@implode("<br />",$teles));
			$this->assign("faxevi",@implode("<br />",$faxes));
		}
		
		// vlasnik, direktor, predstavnik
		$vl = $komitent["komitent_vlasnik_name"];
		$dir = $komitent["komitent_direktor"];
		if(strlen(trim($komitent["komitent_direktor_tel"]))>0){
			$dir .= " [<a href=\"http://1922.168.5.2/gm/call.php\">".$komitent["komitent_direktor_tel"]."</a>]";
		}
		$pr = $komitent["komitent_predst"];
		if(strlen(trim($komitent["komitent_predst_tel"]))>0){
			$pr .= " [".$komitent["komitent_predst_tel"]."]";
		}
		$this->assign("vlasnik",$vl);
		$this->assign("direktor",$dir);
		$this->assign("predstavnik",$pr);
		
		
		echo $this->parseTemplate("rokovnik/komitent_details.tpl");
	}
	function komeDetailsDelAddresses($id){
		$q = "SELECT DISTINCT komdel_adresa_id, delatnost_ime "
			."FROM komitenti_delatnosti kd, delatnosti d, komitenti k "
			."WHERE k.komitent_id=$id "
			."AND k.komitent_id=kd.komdel_kom_id "
			."AND kd.komdel_del_id=d.delatnost_id "
			."AND kd.komdel_adresa_id!=k.komitent_adresa";
		$res = $this->db->select2($q);
		if ($res!==false) {
			foreach ($res as $k=>$v){
				$adr = $this->adresa->getAll($v["komdel_adresa_id"]);
				
				$mesto = $adr["lokacija"]["mesto"]["name"];
				$ptt = $adr["lokacija"]["mesto"]["ptt"];
				if (isset($adr["lokacija"]["opstina"])) {
					$ptt = strlen(trim($adr["lokacija"]["opstina"]["ptt"]))>0?$adr["lokacija"]["opstina"]["ptt"]:$ptt;
					$mesto .= ", ".$adr["lokacija"]["opstina"]["name"];
				}
				if (isset($adr["lokacija"]["naselje"])) {
					$ptt = strlen(trim($adr["lokacija"]["naselje"]["ptt"]))>0?$adr["lokacija"]["naselje"]["ptt"]:$ptt;
					$mesto .= ", ".$adr["lokacija"]["naselje"]["name"];
				}
				$mesto = strlen(trim($ptt))?"$ptt $mesto":$mesto;
				
				$number = '<span class="listNumber">'.($k+1).".</span>";
				
				$adrese[] = $number.$mesto."<br />".$adr["ulica"]." ".$adr["broj"];
			}
			natsort($adrese);
			$info = count($res)." adresa";
			echo '<div class="area"><div style="font-size:10px; color:#aaa;">'.$info.'</div><ol id="more_address"><li>'.implode("</li><li>",$adrese).'</li></ol></div>';
		}else{
			echo 0;
		}
	}
	function callHistory($kom){
		$q = "SELECT rokovnik.*, user_id, radnici.radnik_ime as ime, radnici.radnik_prezime as prezime, time "
			."FROM rokovnik, users, radnici "
			."WHERE users.id=rokovnik.user_id "
			."AND radnici.radnik_id=users.radnik_id "
			."AND komitent_id=$kom "
			."ORDER BY time DESC";
		$res = $this->db->select2($q);
		if($res!==false){
			$user = $this->getUserId();
			$num=0;
			$history=array();
			foreach ($res as $k=>$rok){
				if($rok["user_id"] == $user){
					$num++;
				}
				
				$options = "";
				//$desc = "<div class=\"history_desc\">";
				$desc = "";
				
				// kontakt
				$kontakt = $this->db->getOne("SELECT name FROM rokovnik_kontakt WHERE id=".$rok["kontakt"]." LIMIT 1");
				$desc .= "<strong>$kontakt: </strong>";
				
				// odgovor
				$odgovor = $this->db->getOne("SELECT name FROM rokovnik_odgovor WHERE id=".$rok["odgovor"]." LIMIT 1");
				//$odgovor = $this->get_RokovnikOdgovor($rok["odgovor"]);
				$desc .= "<i>$odgovor</i><br />";
				
				// result
				$q = "SELECT rokovnik_result.*, rokovnik_result_type.name "
					."FROM rokovnik_result,rokovnik_result_type "
					."WHERE rokovnik_result.type=rokovnik_result_type.id "
					."AND rokovnik_id=".$rok["id"];
				//echo $q;
				list($result) = $this->db->select2($q);
				//print_r($result);
				$desc .= "ISHOD: ".$result["name"]." - ".@date($this->getFromConfig("long_date"),$result["time"]);
				if($rok["desc"]){
					$desc .= ", ".$rok["desc"];
				}
				
				// sastanak
				if($result["id"]){
					$q = "SELECT rs.*, rfb.name, radnici.radnik_ime, radnici.radnik_prezime "
						."FROM rokovnik_sastanak rs, rokovnik_feedback rfb, radnici "
						."WHERE rs.fb_id=rfb.id "
						."AND rs.radnik=radnici.radnik_id "
						."AND rs.result_id=".$result["id"];
					$sastanak = $this->db->select2($q);
					if($sastanak[0]){
						$sastanak = $sastanak[0];
						$desc .= "<br />SASTANAK: "
							.$sastanak["radnik_ime"]." ".$sastanak["radnik_prezime"]
							.", "
							.date($this->getFromConfig("long_date"),$sastanak["time"])
							.", "
							.$sastanak["name"];
						if($sastanak["napomena"]){
							$desc .= ", ".$sastanak["napomena"];
						}
					}
				}
			//------------------ options
			
				//napomena
				if(strlen(trim($rok["napomena"]))){
					$nap = '<div class=&quot;area&quot;><pre style=&quot;font-family:Arial; font-size:12px;&quot;><img src=&quot;gui/icons/napomena.gif&quot; style=&quot;margin:0px 5px 5px 0px;&quot; align=&quot;left&quot; />'.$rok["napomena"].'</pre></div>';
					$options .= '<textarea style="display:none;">'.$nap.'</textarea><img src="gui/icons/napomena.gif" onclick="ypAjaxForm(this.previousSibling.value)" />';
				}
				//spec.dogadjaj
				if(strlen(trim($rok["napomena_special"]))){
					$snap = '<div class=&quot;area&quot;><pre style=&quot;font-family:Arial; font-size:12px;&quot;><img src=&quot;gui/icons/napomena_special.gif&quot; style=&quot;margin:0px 5px 5px 0px;&quot; align=&quot;left&quot; />'.$rok["napomena_special"].'</pre></div>';
					$options .= '<textarea style="display:none">'.$snap.'</textarea><img src="gui/icons/napomena_special.gif" onclick="ypAjaxForm(this.previousSibling.value)" />';
					//$options .= '<img src="gui/icons/napomena_special.gif" onclick="ypAjaxForm(\'<div class=area>'.str_replace("<br />","\\n",$rok["napomena_special"]).'</div>\')" />';
				}
				
				$history_i[] = $k;
				$history_options[] = $options;
				$history_ord[] = count($res)-$k;
				$history_time[] = date($this->getFromConfig("long_date"),$rok["time"]);
				$history_user[] = $rok["ime"]." ".$rok["prezime"];
				$history_desc[] = $desc;
				
				//$desc .= "</div>";
			}
			// broj poziva
			echo ($num+1);
			if(count($history_i)){
				echo "{!}";
				//echo implode("{!!}",$history);
				$this->assign("h_i",$history_i);
				$this->assign("h_options",$history_options);
				$this->assign("h_ord",$history_ord);
				$this->assign("h_user",$history_user);
				$this->assign("h_time",$history_time);
				$this->assign("h_desc",$history_desc);
				echo $this->parseTemplate("rokovnik/history_list.tpl");
			}
		}else{
			echo "1{!}";
		}
		//ugovor
		//require_once("komitenti.class.php");
		//komitenti::getUgovori();
		
		$komname = readValFromInput("name");
		$komname = htmlentities($komname);
		$komname = str_replace(" ","%20",$komname);
		
		//-------------------- IZ FOX-a
		/*$foxid = $this->getFoxID($kom);
		$uri = "http://cvet.yellow/yap1/ugovori.fox.php?a=getTelemarketing&id=$foxid&name=$komname";
		$ugovor = implode("",file($uri));
		echo $ugovor;*/
		
		//-------------------- IZ MySQL-a
		echo "{!}".$this->get_KomitentUgovoriList($kom);
	}
	function YapYap(){
		$parent = getFromInput("id");
		$ret = array();
		
		$res = @$this->db->select2("SELECT child FROM komitenti_komitenti WHERE parent=$parent");
		if ($res !== false) {
			foreach ($res as $row){
				$ret[count($ret)] = $row["child"];
			}
		}
		$res = @$this->db->select2("SELECT parent FROM komitenti_komitenti WHERE child=$parent");
		if ($res !== false) {
			foreach ($res as $row){
				$ret[count($ret)] = $row["parent"];
			}
		}
		$ret = array_unique($ret);
		if (count($ret)) {
			foreach ($ret as $li){
				if (strlen(trim($li))>0) {
					$blank = "blankTarget('LoadRSDoc(\'komitenti.php?a=displayOne&id=".$li."\',\'setContent\')')";
					$name = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=$li LIMIT 1");
					echo "\n\t".'<li value="'.$li.'" onclick="'.$blank.'">';
					$blank = "blankTarget('LoadRSDoc(\'rokovnik.php?ef=setRokovnikKomitent&efid=".$li."&efname=".addslashes(urlencode($name))."\',\'setContent\')')";
					echo '<a class="tm" href="javascript:void(0)" onclick="'.$blank.'; event.stopPropagation();"><img src="gui/icons/telemarketing.gif" /></a>';
					echo "<span>$name</span>";
					echo '</li>';
				}
			}
		}
	}
	function showUgovori($kom){
		$ugovori = $this->get_RokovnikUgovori($kom);
		if($ugovori !== false){
			echo "{!}";
			foreach ($ugovori as $ugovor){
				echo "Ugovor: ".$ugovor["id"];
				if($ugovor["storno"]){
					echo " - storno";
				}
				echo "\n";
				$tmp = explode("-",$ugovor["datum"]);
				echo "Datum: ".$tmp[2].".".$tmp[1].".".$tmp[0]."\n";
				echo "Radnik: ".$ugovor["radnik"]."\n";
				echo "Artikli:\n";
				foreach ($ugovor["artikl"] as $name=>$kol){
					echo "   $kol x $name\n";
				}
				echo "\n";
			}
		}
	}	
	function searchSastanak($get){
		$tmp = explode(".",$get["d"]);
		
		if($get["h"]>0){
			$result_start_time = makeUnixTime($get["h"],$get["m"],0,$tmp[1],$tmp[0],$tmp[2]);
			$result_end_time = makeUnixTime($get["h"],$get["m"],0,$tmp[1],$tmp[0],$tmp[2]);
		}else{
			$result_start_time	= makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			$result_end_time	= makeUnixTime(0,0,0,$tmp[1],$tmp[0]+1,$tmp[2]);
		}
		//$result_time = makeUnixTime($get["h"],$get["m"],0,$tmp[1],$tmp[0],$tmp[2]);
		
		$search_kword = "<br />datum: ".$get["d"];
		if($get["h"]>0){
			$search_kword .= " ".$get["h"].":".$get["m"];
		}
		
		$q = "SELECT DISTINCT rr.id, rr.time, rr.desc, k.komitent_id, k.komitent_name "
			."FROM rokovnik r, rokovnik_result rr, komitenti k "
			."WHERE r.id=rr.rokovnik_id "
			."AND k.komitent_id=r.komitent_id "
			."AND rr.type=3 "
			."AND rr.time>=$result_start_time AND rr.time<=$result_end_time ";
		if(isset($get["kom"]) && $get["kom"]>0){
			$q .= "AND r.komitent_id=".$get["kom"]." ";
			$search_kword = "<br />komitent: ".$get["k"].$search_kword;
		}
		
		$q .= "ORDER BY rr.time, k.komitent_name";
		//$search_kword = "komitent: ".$get["k"].", vreme: ".$get["d"]." ".$get["h"].":".$get["m"];
		$this->assign("search_kword",$search_kword);
			
		//echo $q;
		$res = $this->db->select2($q);
		if($res[0]){
			$ids = array();
			foreach ($res as $k=>$r){
				if(!$this->db->getOne("SELECT count(*) FROM rokovnik_sastanak WHERE result_id=".$r["id"])){
					$ids[] = $k+1;
					
					$kom_ids[] = $r["komitent_id"];
					$kom_name = $r["komitent_name"];
					$time = date("H:i",$r["time"]);
					
					$items[] = "<span style=\"float:right\">$time</span>$kom_name";
					
					$desc[] = $r["desc"];
					//$action[] = "setMeeting('".$r["id"]."','".$get["k"].", ".date($this->getFromConfig("long_date"),$r["time"])."'); closeYpAjaxForm();";
					$action[] = "setMeeting('".$r["id"]."','".addslashes($get["k"]).", ".date($this->getFromConfig("long_date"),$r["time"])."'); closeYpAjaxForm();";
				}
			}
			if(count($ids)){
				$this->assign("list_num",$kom_ids);
				$this->assign("list_item",$items);
				$this->assign("liaction",$action);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->");
				$this->assign("search_info","Nema zakazanih sastanaka za zadate kriterijume ili je sastanak već održan.");
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
			$this->assign("search_info","Nema zakazanih sastanaka za zadate kriterijume ili je sastanak već održan.");
			//echo "Nema rezultata";
		}
		echo $this->parseTemplate("rokovnik/search_list.tpl");
	}	
	function addMeetingFeedback($get){
		$get["n"] = utf8RawUrlDecode($get["n"]);
		$get["n"] = utf8RawChrDecode($get["n"]);
		
		//print_rarray($get);die();
		
		
		$tmp = explode(".",$get["date"]);
		if($tmp[2] >37 && $tmp[2]<70){
			$tmp[2] = date("Y");
		}
		$time = makeUnixTime($get["h"],$get["m"],0,$tmp[1],$tmp[0],$tmp[2]);
		
		$tmp = explode(".",$get["date1"]);
		//print_r($tmp);
		if($tmp[2] >37 && $tmp[2]<70){
			$tmp[2] = date("Y");
		}
		$time2 = makeUnixTime($get["h1"],$get["m1"],0,$tmp[1],$tmp[0],$tmp[2]);
		//echo $time." - ".date("d.m.Y H:i:s",$time)."\n";
		//print_r($get);
		
		if($get["izmena"] == 1){
			// IZMENA SASTANKA
			$q = "UPDATE rokovnik_sastanak SET "
				."result_id='".$get["id"]."',"
				."fb_id='".$get["fb"]."',"
				."time='".$time."',"
				."time1='".$get["unet_time"]."',"
				."time2='$time2',"
				."radnik='".$get["r"]."',"
				."user='".$_COOKIE["ypu"]."',"
				."napomena='".$get["n"]."' "
				."WHERE id=".$get["theID"];
			$msg = "Izmena izvršena.";
		}else{
			// NOVI SASTANAK
			$q = "INSERT INTO rokovnik_sastanak VALUES('',"
				.$get["id"].","
				.$get["fb"].","
				.$time.","
				.time().","
				.$time2.", "
				.$get["r"].","
				.$_COOKIE["ypu"].","
				."'".$get["n"]."')";
			$msg = "Novi sastanak uspešno dodat.";
		}
		//echo $q;die();
		if(@$this->db->alterTable($q)){
			echo $msg;
		}else{
			echo "GREŠKA! Kontaktirajte administratora.\n\n".str_replace("<br />","\n",mysql_error());
		}
	}	
	function deleteMeeting($id){
		if($this->db->alterTable("DELETE FROM rokovnik_sastanak WHERE id=$id")){
			echo "Brisanje izvršeno.";
		}else{
			echo mysql_error();
		}
	}	
	function browseMeetings($post){
		$polja = $this->get_Radnici($post["radnik"]);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		
		// assign values for meeting date/time
		$this->assign("sastanak_date",$post["sastanak_date"]);
		$this->assign("sastanak_hour",$post["sastanak_hour"]);
		$this->assign("sastanak_minute",$post["sastanak_minute"]);
		
		$start=false;
		if(!strlen(trim($post["sastanak_date"]))){
			$start_time = makeUnixTime(0,0,0,date("m"),date("d"),date("y"));
			$end_time = $start_time+60*60*24;
			//echo date("d.m.Y H:i:s",$start_time)."<br />".date("d.m.Y H:i:s",$end_time);
			$start=true;
			$this->assign("sastanak_date",date("d.m.Y",$start_time));
		}else{
			$tmp = explode(".",$post["sastanak_date"]);
			if($post["sastanak_hour"]>0 || $post["sastanak_minute"]>0){
				$time = makeUnixTime($post["sastanak_hour"],$post["sastanak_minute"],0,$tmp[1],$tmp[0],$tmp[2]);
			}else{
				$start = true;
				$start_time = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				$end_time = makeUnixTime(0,0,0,$tmp[1],$tmp[0]+1,$tmp[2]);
			}
		}
		$q = "SELECT rs.*,rfb.name "
			."FROM rokovnik_sastanak rs, rokovnik_feedback rfb "
			."WHERE rs.fb_id=rfb.id "
			//."AND user=".$_COOKIE["ypu"]." "
			."AND ";
		if($start){
			$q .= "(time1>$start_time AND time1<$end_time) ";
		}else{
			$q .= "time1=$time";
		}
		
		if($post["radnik"]>0){
			$q .= " AND radnik=".$post["radnik"];
		}
		//echo $q;
		$res = $this->db->select2($q);
		if($res[0]){
			foreach ($res as $row){
				$sasid[] = $row["id"];
				$resid[] = $row["result_id"];
				
				$q = "SELECT k.komitent_name, r.time, rr.time as ztime "
					."FROM komitenti k, rokovnik r, rokovnik_result rr "
					."WHERE k.komitent_id=r.komitent_id "
					."AND r.id=rr.rokovnik_id "
					."AND rr.id=".$row["result_id"];
				
				list($rinfo) = $this->db->select2($q);
				$info[] = "<strong>".$rinfo["komitent_name"]."</strong>"
						."<br /><span style=\"display:block; float:left; text-align:right; width:60px;\">pozvano:</span><span style=\"display:block; float:left;\">".date($this->getFromConfig("long_date"),$rinfo["time"])."</span>"
						."<br clear=\"both\" /><span style=\"display:block; text-align:right; float:left; width:60px;\">zakazano:</span><span style=\"display:block; float:left;\">".date($this->getFromConfig("long_date"),$rinfo["ztime"])."</span>";
				
				$fb[] = $row["name"]."<br />".@date($this->getFromConfig("long_date"),$row["time2"]);
				$t[] = @date($this->getFromConfig("long_date"),$row["time"]);
				$t1[] = @date($this->getFromConfig("long_date"),$row["time1"]);
				
				$q = "SELECT concat(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$row["radnik"]." LIMIT 1";
				$r[] = $this->db->getOne($q);
				
				$napomena[] = str_replace("\n","<br />",$row["napomena"]);
			}
			$this->assign("sastanak_id",$sasid);
			$this->assign("result_id",$resid);
			$this->assign("rinfo",$info);
			$this->assign("feedback",$fb);
			$this->assign("time_odrzan",$t);
			$this->assign("time_unet",$t1);
			$this->assign("radnik",$r);
			$this->assign("napomena",$napomena);
		}else{
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->");
			$this->assign("message","Nema evidencija o sastanku.");
		}
		
		echo $this->parseTemplate("rokovnik/browse_meetings.tpl");
	}
	
	function newMeetingTicket(){
		foreach ($_POST as $k=>$v){
			$_POST[$k] = readValFromInput($k);
		}
		
		$this->assign("naslov","Novi listić");
		$this->assign("ticketid","");
		$this->assign("comment_start_lozinka_manager","<!--");
		//$this->assign("comment_end_lozinka_manager","-->");
		
		
		$komid			= $_POST["komid"];
		$meeting_date	= $_POST["mD"];
		$meeting_hour	= $_POST["mH"];
		$meeting_minute	= $_POST["mM"];
		$napomena		= $_POST["napomena"];
		$kontakt		= $_POST["kontakt"];
		$poreklo		= $_POST["poreklo"];
		
		// komitent
		$q = "SELECT DISTINCT komitent_name, komitent_vlasnik_name, komitent_direktor, komitent_predst, komitent_adresa, komdel_adresa_id FROM komitenti k, komitenti_delatnosti kd WHERE k.komitent_id=kd.komdel_kom_id AND komitent_id=$komid";
		$kontakt = $kontakt_func = array();
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $row){
				$adrese[] = $row["komitent_adresa"];
				$adrese[] = $row["komdel_adresa_id"];
				$komitent_name = $row["komitent_name"];
				
				$kontakt[] = $row["komitent_vlasnik_name"]."{!}vlasnik";
				$kontakt[] = $row["komitent_direktor"]."{!}direktor";
				$kontakt[] = $row["komitent_predst"]."{!}predstavnik";
			}
			
			$kontakt = array_unique($kontakt);
			foreach ($kontakt as $k=>$v){
				$tmp = explode("{!}",$v);
				if (strlen(trim($tmp[0]))==0) {
					unset($kontakt[$k]);
				}else{
					$kontakt[$k] = $tmp[0];
					$kontakt_func[$k] = $tmp[1];
				}
			}
			$this->assign("kontakt",count($kontakt)>0?array_values($kontakt):"");
			$this->assign("kontakt_func",count($kontakt_func)>0?array_values($kontakt_func):"");
			
			$this->assign("komitent_id",$komid);
			$this->assign("komitent",$komitent_name);
			
			$adrese = array_unique($adrese);
			$q = "SELECT adresa_id, ulica_ime, adresa_broj, lok_naziv, lok_tip "
				."FROM adrese, ulice, lokacije "
				."WHERE adrese.adresa_ul_id=ulice.ulica_id "
				."AND adresa_lid=lokacije.lid "
				."AND (";
			
			foreach ($adrese as $adr){
				$q .= "adrese.adresa_id=$adr OR ";
			}
			$q = substr($q,0,strlen($q)-4).") ";
			$q .= "ORDER BY lok_naziv, ulica_ime";
			
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$ad[] = $row["lok_naziv"].", ".$row["ulica_ime"]." ".$row["adresa_broj"];
					$ad_id[] = $row["adresa_id"];
				}
			}
			
			// telefoni
			$q = "SELECT DISTINCT telefon FROM komitenti_telefoni WHERE kom_id=$komid ORDER BY telefon";
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					if (preg_match('/\d/',$row["telefon"])) {
						$tels[] = $row["telefon"];
					}
				}
			}
			
			// poreklo
			$porekla = $this->get_RokovnikPoreklo($poreklo);
				$this->assign("poreklo_id",$porekla["polje_id"]);
				$this->assign("poreklo_name",$porekla["polje_ime"]);
				$this->assign("poreklo_selected",$porekla["polje_selected"]);
				
			// delatnosti
			$q = "SELECT DISTINCT delatnost_ime FROM delatnosti, komitenti_delatnosti WHERE komdel_del_id=delatnost_id AND komdel_kom_id=$komid";
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$komdels[] = $row["delatnost_ime"];
				}
			}
			
			// radnici
			$polja = $this->get_Radnici(null,1);
				$this->assign("radid",$polja["polje_id"]);
				$this->assign("radnik_ime",$polja["polje_ime"]);
				$this->assign("radnik_prezime",$polja["polje_prezime"]);
		}
		
		$this->assign("mt_date",$meeting_date);
		$this->assign("mt_hour",$meeting_hour);
		$this->assign("mt_minute",$meeting_minute);
		
		$this->assign("adresa",$ad);
		$this->assign("adresa_id",$ad_id);
		$this->assign("telefon",$tels);
		$this->assign("komdels",implode("\n",$komdels));
		
		$this->assign("napomena",$napomena);
		
		
		echo $this->parseTemplate("rokovnik/meeting_ticket_new.tpl");
		//echo "<div class=\"area\">"; print_rarray($_POST); echo "</div>";
	}
	function editMeetingTicket(){
		
		$id = getFromInput("id");
		
		$q = "SELECT rsl.*, k.komitent_name "
			."FROM rokovnik_sastanak_listici rsl, komitenti k "
			."WHERE rsl.id=$id "
			."AND k.komitent_id=rsl.komitent";
		$res = $this->db->select2($q);
		$res = $res[0];
		
		
		// komitent
		$this->assign("komitent",$res["komitent_name"]);
		$this->assign("komitent_id",$res["komitent"]);
		
		
		// adrese
		$q = "SELECT DISTINCT komitent_adresa, komdel_adresa_id FROM komitenti k, komitenti_delatnosti kd WHERE k.komitent_id=kd.komdel_kom_id AND k.komitent_id=".$res["komitent"];
		$adr_res = $this->db->select2($q);
		$adrese = array();
		foreach ($adr_res as $a){
			$adrese[] = $a["komitent_adresa"];
			$adrese[] = $a["komdel_adresa_id"];
		}
		$adrese = array_unique($adrese);
		$q = "SELECT adresa_id, ulica_ime, adresa_broj, lok_naziv, lok_tip "
				."FROM adrese, ulice, lokacije "
				."WHERE adrese.adresa_ul_id=ulice.ulica_id "
				."AND adresa_lid=lokacije.lid "
				."AND (";
		foreach ($adrese as $adrid){
			$q .= "adresa_id=$adrid OR ";
		}
		$q = substr($q,0,strlen($q)-4).") ORDER BY lok_naziv, ulica_ime";
		$ad = $this->db->select2($q);
		$ima = false;
		foreach ($ad as $adr){
			$adrese_ids[] = $adr["adresa_id"];
			 $tmpa = $adr["lok_naziv"].", ".$adr["ulica_ime"]." ".$adr["adresa_broj"];
			 $adrese_names[] = $tmpa;
			if ($tmpa == $res["adresa"]) {
				$ima = true;
				$adrese_selected[] = " selected";
			}else{
				$adrese_selected[] = "";
			}
		}
		if (!$ima) {
			$this->assign("adresa_other",$res["adresa"]);
		}
		$this->assign("adresa_id",$adrese_ids);
		$this->assign("adresa",$adrese_names);
		$this->assign("adresa_selected",$adrese_selected);
		
		
		// delatnosti
		$this->assign("komdels",$res["delatnosti"]);
		
		// kontakti
		$q = "SELECT komitent_vlasnik_name, komitent_direktor, komitent_predst FROM komitenti WHERE komitent_id=".$res["komitent"];
		$kontakti_res = $this->db->getRow($q);
		$kontakti=$kontakti_func=array();
		if (strlen(trim($kontakti_res[0]))) {
			$kontakti[] = trim($kontakti_res[0]);
			$kontakti_func[] = "vlasnik";
			if (trim($kontakti_res[0])==$res["kontakt"] && $res["kontakt_fja"]=="vlasnik") {
				$kontakti_selected[] = " selected";
			}else{
				$kontakti_selected[] = "";
			}
		}
		if (strlen(trim($kontakti_res[1]))) {
			$kontakti[] = trim($kontakti_res[1]);
			$kontakti_func[] = "direktor";
			if (trim($kontakti_res[1])==$res["kontakt"] && $res["kontakt_fja"]=="direktor") {
				$kontakti_selected[] = " selected";
			}else{
				$kontakti_selected[] = "";
			}
		}
		$ima = false;
		if (strlen(trim($kontakti_res[2]))) {
			$kontakti[] = trim($kontakti_res[2]);
			$kontakti_func[] = "predstavnik";
			if (trim($kontakti_res[2])==$res["kontakt"] && $res["kontakt_fja"]=="predstavnik") {
				$ima = true;
				$kontakti_selected[] = " selected";
			}else{
				$kontakti_selected[] = "";
			}
		}
		if (count($kontakti)==0) {$kontakti="";}
		if (count($kontakti_func)==0) {$kontakti_func="";}		
		if (!$ima) {
			$this->assign("kontakt_other",$res["kontakt"]);
		}
		$this->assign("kontakt",$kontakti);
		$this->assign("kontakt_func",$kontakti_func);
		$this->assign("kontakt_selected",$kontakti_selected);
		$this->assign("kontakt_fja",$res["kontakt_fja"]);
		
		// poreklo
		$polja = $this->get_RokovnikPoreklo($res["poreklo"]);
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo_name",$polja["polje_ime"]);
			$this->assign("poreklo_selected",$polja["polje_selected"]);
			
		// telefoni
		$q = "SELECT DISTINCT telefon FROM komitenti_telefoni WHERE kom_id=".$res["komitent"]." ORDER BY telefon";
		$res_tel = $this->db->select2($q);
		if ($res_tel !== false) {
			$tels = $tels_selected = array();
			$ima = false;
			foreach ($res_tel as $row){
				if (preg_match('/\d/',$row["telefon"])) {
					$tels[] = $row["telefon"];
					if ($row["telefon"]==$res["telefon"]) {
						$ima = true;
						$tels_selected[] = " selected";
					}else{
						$tels_selected[] = "";
					}
				}
			}
			if (!$ima) {
				$this->assign("telefon_other",$res["telefon"]);
			}
			$this->assign("telefon",$tels);
			$this->assign("telefon_selected",$tels_selected);
		}
		
		// vreme sastanka
		if ($res["ticket_time"]>0) {
			$this->assign("mt_date",date("d.m.Y",$res["ticket_time"]));
			$this->assign("mt_hour",date("H",$res["ticket_time"]));
			$this->assign("mt_minute",date("i",$res["ticket_time"]));
		}
		
		// komercijalista
		$this->assign("komer_prev",$res["komercijalista"]);
		$polja = $this->get_Radnici($res["komercijalista"],1);
			$this->assign("radid",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
			
		// manager
		$this->assign("manager_prev",$res["manager"]);
		$polja = $this->get_Radnici($res["manager"],1);
			$this->assign("manid",$polja["polje_id"]);
			$this->assign("manager_ime",$polja["polje_ime"]);
			$this->assign("manager_prezime",$polja["polje_prezime"]);
			$this->assign("manager_selected",$polja["polje_selected"]);
		
		// napomena
		$this->assign("napomena",$res["napomena"]);
		
		$this->assign("naslov","Izmena listića");
		$this->assign("ticketid",$id);
		echo $this->parseTemplate("rokovnik/meeting_ticket_new.tpl");
	}
	function saveMeetingTicket(){
		
		$ticketid = getFromInput("ticketid");
		if ($ticketid>0) {
			$new = false;
		}else{
			$new = true;
		}
		
		$user	= $this->getUserId();
		$time	= time();
		$komid	= getFromInput("mt_komitent_id");
		
		$adr	= readValFromInput("mt_adresa");
		$adr1	= readValFromInput("adresa_other");
		if ($adr=="-1") {
			$adr = $adr1;
		}
		//die("$adr\n$adr1");
		
		$dels	= readValFromInput("mt_komdels");
		
		$kont	= readValFromInput("mt_kontakt");
		$kont1	= readValFromInput("mt_kontakt_other");
		if ($kont == -1) {
			$kont = $kont1;
		}else{
			$kont = substr($kont,0,strpos($kont,"{!}"));
		}
		
		$kont_f	= readValFromInput("mt_kontakt_funkcija");
		$poreklo= readValFromInput("mt_poreklo");
		
		$tel	= readValFromInput("mt_telefon");
		$tel1	= readValFromInput("telefon_other");
		if ($tel == -1) {
			$tel = $tel1;
		}
		
		$mt_date= getFromInput("mt_date");
		$mt_h	= getFromInput("mt_hour");
		$mt_m	= getFromInput("mt_minute");
		if (strlen($mt_date)) {
			$mt_date = explode(".",$mt_date);
			$mt_time= makeUnixTime($mt_h,$mt_m,0,$mt_date[1],$mt_date[0],$mt_date[2]);
		}else{
			$mt_time=0;
		}
		
		
		$komer	= getFromInput("mt_komercijalista");
		$nap	= readValFromInput("mt_napomena");
		
		
		if ($new) {
			$q = "INSERT INTO "
				."rokovnik_sastanak_listici(user, time, komitent, adresa, delatnosti, komercijalista, kontakt, kontakt_fja, poreklo, telefon, ticket_time, napomena) "
				."VALUES("
				."$user,$time,$komid,'".addslashes($adr)."',"
				."'".addslashes($dels)."', $komer,"
				."'".addslashes($kont)."','".addslashes($kont_f)."',"
				."'".addslashes($poreklo)."','".addslashes($tel)."',"
				."$mt_time,'".addslashes($nap)."');";
		}else{
			$manager_prev = intval(getFromInput("mt_manager_prev"));
			$komer_prev = intval(getFromInput("mt_komer_prev"));
			$manager = getFromInput("mt_manager");
			$password = readValFromInput("mt_lozinka_manager");
			$superpass = $this->getFromConfig("superpass");
			$confirm=1;
			
			$test = $manager_prev>0 && ($manager!=$manager_prev || $komer!=$komer_prev);
			//echo "test:";var_dump($test);die();
			
			//var_dump($this->checkSuperPass($password));
			//var_dump($test);
			//die();
			
			if ($test) {
				$sql = "SELECT 1 FROM users WHERE radnik_id=$manager_prev AND password=OLD_PASSWORD('$password')";
				$confirm = $this->db->getOne($sql);
				if ($confirm!==null || $this->checkSuperPass($password)) {
					$confirm = 1;
				}else{
					die("Pogrešna lozinka za menadžera koji poseduje listić!");
				}
			}
			
			$q = "UPDATE rokovnik_sastanak_listici SET "
				."user=$user,"
				."time=$time,"
				."komitent=$komid,"
				."adresa='".addslashes($adr)."',"
				."delatnosti='".addslashes($dels)."',"
				."komercijalista=$komer,"
				."manager=$manager,"
				."kontakt='".addslashes($kont)."',"
				."kontakt_fja='".addslashes($kont_f)."',"
				."poreklo='".addslashes($poreklo)."',"
				."telefon='".addslashes($tel)."',"
				."ticket_time=$mt_time,"
				."napomena='".addslashes($nap)."' "
				."WHERE id=$ticketid LIMIT 1";
			//die("$q");
		}
			
		if($this->db->alterTable($q)){
			echo "Listić sačuvan.";
		}else{
			echo mysql_error();
		}
		
	}
	function deleteMeetingTicket(){
		
		$user = $this->getUserId();
		$id=getFromInput("id");
		$superpass=$this->getFromConfig("superpass");
		$pass = readValFromInput("pass");
		$usercreated = getFromInput("userCreated");
		
		$man = $this->db->getOne("SELECT manager FROM rokovnik_sastanak_listici WHERE id=$id LIMIT 1");
		$manpass = $this->db->getOne("SELECT 1 FROM users WHERE radnik_id=$man AND password=OLD_PASSWORD('$pass')");
		 file_put_contents("tmpVOJA",md5($pass));
		$wrongpass=false;
		if ($man>0){
			if(!$this->checkSuperPass($pass) && $manpass!=1) {
				$wrongpass=true;
			}
		}
		if ($wrongpass) {
			die("Uneli ste pogrešnu lozinku.");
		}
		$userpass = $this->db->getOne("SELECT 1 FROM users WHERE id=$usercreated AND password=OLD_PASSWORD('$pass')");
		if ($user!=$usercreated) {
			if (!($this->checkSuperPass($pass) || $userpass)) {
				die("Niste autorizovani za brisanje listića");
			}
		}
		$q = "DELETE FROM rokovnik_sastanak_listici WHERE id=$id LIMIT 1";
		if ($this->db->alterTable($q)) {
			echo "Listić je izbrisan.";
		}else{
			echo mysql_error();
		}
	}
	function browseMeetingTickets(){
		$user = $this->getUserId();
		$userLevel = $this->getUserLevel();
		if($userLevel["name"] != "admin"){
			$this->assign("diz_admin","disabled");
		}
		
		$fKomitent = readValFromInput("filter_komitent");
		$this->assign("filter_komitent",$fKomitent);
		
		$fRadnik = getFromInput("filter_komercijalista");
		$polja = $this->get_Radnici($fRadnik,1);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		
		$fManager = getFromInput("filter_manager");
		$polja = $this->get_Radnici($fManager,1);
			$this->assign("manager_id",$polja["polje_id"]);
			$this->assign("manager_ime",$polja["polje_ime"]);
			$this->assign("manager_prezime",$polja["polje_prezime"]);
			$this->assign("manager_selected",$polja["polje_selected"]);
		
		$fAdresa = readValFromInput("filter_adresa");
		$this->assign("filter_adresa",$fAdresa);
		
		$fDate = getFromInput("filter_date");
		$fHour = getFromInput("filter_hour");
		$fMinu = getFromInput("filter_minute");
		$fDateDO = getFromInput("filter_date_do");
		$fHourDO = getFromInput("filter_hour_do");
		$fMinuDO = getFromInput("filter_minute_do");
		
		if (strlen($fDate)>0 && !strlen($fHour) && !strlen($fMinu)) {
			$fHour = 0;
			$fMinu = 0;
		}
		if (strlen($fDateDO)>0 && !strlen($fHourDO) && !strlen($fMinuDO)) {
			$fHourDO = 23;
			$fMinuDO = 59;
		}
		
		$fDateCall = getFromInput("filter_date_call");
		$fHourCall = getFromInput("filter_hour_call");
		$fMinuCall = getFromInput("filter_minute_call");
		$fDateDOCall = getFromInput("filter_date_do_call");
		$fHourDOCall = getFromInput("filter_hour_do_call");
		$fMinuDOCall = getFromInput("filter_minute_do_call");
		
		if (strlen($fDateCall)>0 && !strlen($fHourCall) && !strlen($fMinuCall)) {
			$fHourCall = 0;
			$fMinuCall = 0;
		}
		if (strlen($fDateDOCall)>0 && !strlen($fHourDOCall) && !strlen($fMinuDOCall)) {
			$fHourDOCall = 23;
			$fMinuDOCall = 59;
		}
		
		$qTimeSastanak = !strlen($fDate) && !strlen($fDateDO) && !$fHour && !$fHourDO && !$fMinu && !$fMinuDO;
		$qTimeCall = !strlen($fDateCall) && !strlen($fDateDOCall) && !$fHourCall && !$fHourDOCall && !$fMinuCall && !$fMinuDOCall;
		//echo "Sastanak: ";var_dump($qTimeSastanak);
		//echo "<br />Call: ";var_dump($qTimeCall);
		if ($qTimeSastanak && $qTimeCall) {
			$fDate = date("d.m.Y");
		}
		
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDateCall)) {
			$tmp = explode(".",$fDateCall);
			$fTimeCall = makeUnixTime($fHourCall,$fMinuCall,0,$tmp[1],$tmp[0],$tmp[2]);
		}else{
			$fTimeCall = 0;
		}
		
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDate)) {
			$tmp = explode(".",$fDate);
			$fTime = makeUnixTime($fHour,$fMinu,0,$tmp[1],$tmp[0],$tmp[2]);
		}else{
			$fTime = 0;
		}
		
		if ($fTimeCall==0 && $fTime==0) {
			$fTime = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
			$fDate = date("d.m.Y");
		}
		
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDateDO)) {
			$tmp = explode(".",$fDateDO);
			$fTimeDO = makeUnixTime($fHourDO,$fMinuDO,0,$tmp[1],$tmp[0],$tmp[2]);
		}else{
			$fTimeDO = 0;
		}
		
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$fDateDOCall)) {
			$tmp = explode(".",$fDateDOCall);
			$fTimeDOCall = makeUnixTime($fHourDOCall,$fMinuDOCall,0,$tmp[1],$tmp[0],$tmp[2]);
		}else{
			$fTimeDOCall = 0;
		}
		
		$this->assign("filter_date",$fDate);
		$this->assign("filter_hour",strval($fHour));
		$this->assign("filter_minute",strval($fMinu));
		$this->assign("filter_date_do",$fDateDO);
		$this->assign("filter_hour_do",strval($fHourDO));
		$this->assign("filter_minute_do",strval($fMinuDO));
		
		$this->assign("filter_date_call",$fDateCall);
		$this->assign("filter_hour_call",strval($fHourCall));
		$this->assign("filter_minute_call",strval($fMinuCall));
		$this->assign("filter_date_do_call",$fDateDOCall);
		$this->assign("filter_hour_do_call",strval($fHourDOCall));
		$this->assign("filter_minute_do_call",strval($fMinuDOCall));
		
		
		$q = "SELECT rsl.*, rp.name as poreklo_name, komitent_name "
			."FROM rokovnik_sastanak_listici rsl, rokovnik_poreklo rp, komitenti k "
			."WHERE rsl.komitent=k.komitent_id "
			."AND rp.id=rsl.poreklo ";
			
		if (strlen($fKomitent)>0) {
			if (preg_match('/[^\d]/',$fKomitent)) {
				$q .= "AND komitent_name LIKE '%".addslashes($fKomitent)."%' ";
			}else{
				$q .= "AND komitent_id='".addslashes($fKomitent)."' ";
			}
		}
		if ($fRadnik>-2) {
			if ($fRadnik==-1) {
				$q .= "AND (komercijalista=-1 OR komercijalista=0) ";
				$this->assign("selected_bez_radnik"," selected");
			}elseif ($fRadnik>0){
				$q .= "AND komercijalista=$fRadnik ";
			}
		}
		if ($fManager>-2) {
			if ($fManager==-1) {
				$q .= "AND (manager=-1 OR manager=0) ";
				$this->assign("selected_bez_manager"," selected");
			}elseif ($fManager>0){
				$q .= "AND manager=$fManager ";
			}
		}
		if (strlen($fAdresa)>0) {
			$q .= "AND adresa LIKE '%$fAdresa%' ";
		}
		if ($fTime>0) {
			$q .= "AND rsl.ticket_time>=$fTime ";
		}
		if ($fTimeDO>0 && $fTimeDO>=$fTime) {
			$q .= "AND rsl.ticket_time<=$fTimeDO ";
		}
		if ($fTimeCall>0) {
			$q .= "AND rsl.time>=$fTimeCall ";
		}
		if ($fTimeDOCall>0 && $fTimeDOCall>=$fTimeCall) {
			$q .= "AND rsl.time<=$fTimeDOCall ";
		}
		
		
		$q .= "ORDER BY ticket_time ASC, adresa ASC";
		//echo $q;
		$res = $this->db->select2($q);
		if ($res !== false) {
			
			$this->assign("info","Ukupno: ".count($res));
			$num=0;
			$komman = array();
			//print_rarray($res);
			foreach ($res as $row){
				$nums[] = ++$num;
				$ids[] = $row["id"];
				$names[] = $row["komitent_name"];
				$poreklo[] = $row["poreklo_name"];
				$komerci_id[] = $row["komercijalista"];
				
				$km = $komerci_ime = $komerci_prezime = $manager_ime = $manager_prezime = "";
				if ($row["komercijalista"]>0) {
					list($komerci_ime,$komerci_prezime) = $this->db->getRow("SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$row["komercijalista"]." LIMIT 1");
				}
				if ($row["manager"]>0) {
					list($manager_ime,$manager_prezime) = $this->db->getRow("SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$row["manager"]." LIMIT 1");
					
					$zauzet[] = "da";
					$disabled_checkbox[] = "";
					$km = "<span title=\"menadžer\">".$manager_ime." ".$manager_prezime."</span>";
					if (strlen($komerci_ime) || strlen($komerci_prezime)) {
						$km .= " / <span title=\"komercijalista\">".$komerci_ime." ".$komerci_prezime."</span>";
					}
					$km .= "<br />";
					$editBtn[] = "none";
				}else{
					$zauzet[] = "ne";
					$disabled_checkbox[] = " style=\"display:none;\"";
					
					if (strlen($komerci_ime) || strlen($komerci_prezime)) {
						$km = "<span title=\"komercijalista\">".$komerci_ime." ".$komerci_prezime."</span><br />";
					}
					
					$editBtn[] = "block";
				}
				$komman[] = $km;
				
				list($user_ime,$user_prezime) = $this->db->getRow("SELECT radnik_ime, radnik_prezime FROM radnici, users WHERE users.radnik_id=radnici.radnik_id ANd users.id=".$row["user"]);
				$users[] = $user_ime." ".$user_prezime;
				$userCreated[] = $row["user"];
				
				$adrese[] = $row["adresa"];
				$telefoni[] = $row["telefon"];
				$datumi[] = date($this->getFromConfig("long_date2"),$row["ticket_time"]);
				$datumi_poziva[] = date($this->getFromConfig("long_date2"),$row["time"]);
				if (strlen($row["napomena"])) {
					/*$nap = str_replace('"',"'",$row["napomena"]);
					$nap = addslashes($nap);
					$nap = preg_replace('/\r*\n+/','\n',$row["napomena"]);
					$napomena[] = '<img title="Napomena..." src="gui/icons/napomena.gif" style="cursor:pointer" onclick="alert(\''.$nap.'\')" />';*/
					$napomena[] = nl2br($row["napomena"]);
				}else{
					$napomena[] = "";
				}
			}			
			$this->assign("num",$nums);
			$this->assign("id",$ids);
			$this->assign("name",$names);
			$this->assign("zauzet",$zauzet);
			$this->assign("disabled_checkbox",$disabled_checkbox);
			$this->assign("adresa",$adrese);
			$this->assign("vreme",$datumi);
			$this->assign("user",$users);
			$this->assign("userCreated",$userCreated);
			$this->assign("vreme_poziva",$datumi_poziva);
			$this->assign("poreklo",$poreklo);
			$this->assign("telefon",$telefoni);
			$this->assign("manager_komercijalista",$komman);
			$this->assign("komercijalista_id",$komerci_id);
			$this->assign("editBtnDisplay",$editBtn);
			$this->assign("napomena",$napomena);
			
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end_nores","-->No results.");
		}
		
		$userlevel = $this->getUserLevel();
		if ($userlevel["rights"]["sastanak_listici"]==0) {
			$this->assign("comment_start_user_rights","<!--");
			$this->assign("comment_end_user_rights","-->");
		}
		
		echo $this->parseTemplate("rokovnik/meeting_ticket.tpl");
	}
	function updateMTKomercijalista(){
		$komid = getFromInput("updateTicket_komercijalista");
		$ticket = getFromInput("ticket");
		
		$user = $this->getUserId();
		$manid = $this->db->getOne("SELECT radnik_id FROm users WHERE id=$user LIMIT 1");
		//die("Radnik: $radid");
		
		if ($this->db->alterTable("UPDATE rokovnik_sastanak_listici SET manager=$manid, komercijalista=$komid WHERE id=$ticket LIMIT 1")) {
			echo "Listić izmenjen";
		}else {
			echo mysql_error();
		}
	}
	function generateMeetingTickets(){
		$ids = explode(";",getFromInput("ids"));
		$out = "";
		foreach ($ids as $id){
			$q = "SELECT DISTINCT d.delatnost_ime, k.komitent_name, k.komitent_matbr, k.komitent_pib, rsl.adresa, rsl.kontakt, rsl.kontakt_fja, rp.name as poreklo, rsl.telefon, rsl.ticket_time, rsl.napomena, rsl.komercijalista "
				."FROM rokovnik_sastanak_listici rsl, komitenti k, komitenti_delatnosti kd, delatnosti d, rokovnik_poreklo rp "
				."WHERE k.komitent_id=rsl.komitent "
				."AND k.komitent_id=kd.komdel_kom_id "
				."AND d.delatnost_id=kd.komdel_del_id "
				."AND rp.id=rsl.poreklo "
				."AND rsl.id=$id";
			$res = $this->db->select2($q);
			if ($res !== false) {
				$delatnosti = array();
				foreach ($res as $row){
					if (strlen($row["delatnost_ime"]>25)) {
						$row["delatnost_ime"] = substr($row["delatnost_ime"],0,10)."...".substr($row["delatnost_ime"],strlen($row["delatnost_ime"])-10);
					}
					$delatnosti[] = $row["delatnost_ime"];
				}
				
				$komercijalista_id = $res[0]["komercijalista"];
				if ($komercijalista_id>0) {
					$komercijalista = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$komercijalista_id");
				}else{
					$komercijalista = "nema";
				}
				
				$komitent_name	= $res[0]["komitent_name"];
				$pib = $res[0]["komitent_pib"];
				$matbr = $res[0]["komitent_matbr"];
				$komitent_matbr	= strlen($matbr)>0?"<strong>MATBR:</strong>$matbr ":"";
				$komitent_pib	= strlen($pib)>0?strlen($matbr)>0?", <strong>PIB:</strong>$pib ":"<strong>PIB:</strong>$pib ":"";
				if (strlen($matbr) || strlen($pib)) {
					$this->assign("br","<br />");
				}
				$adresa			= $res[0]["adresa"];
				$kontakt		= $res[0]["kontakt"];
				$kontakt_fja	= $res[0]["kontakt_fja"];
				$poreklo		= $res[0]["poreklo"];
				$telefon		= $res[0]["telefon"];
				$time			= $res[0]["ticket_time"];
				$napomena		= $res[0]["napomena"];
				
				$this->assign("komitent_name",$komitent_name);
				$this->assign("komitent_matbr",$komitent_matbr);
				$this->assign("komitent_pib",$komitent_pib);
				$this->assign("komitent_adresa",$adresa);
				$this->assign("komdels",implode(", ",$delatnosti));
				$this->assign("kontakt_name",$kontakt);
				$this->assign("kontakt_fja",$kontakt_fja);
				$this->assign("phone",$telefon);
				$this->assign("datetime",date($this->getFromConfig("long_date2"),$time));
				$this->assign("poreklo",$poreklo);
				$this->assign("komercijalista",$komercijalista);
				$this->assign("komentar",$napomena);
				
				//$out .= "\r\n".$this->parseTemplate("rokovnik/meeting_ticket_template.tpl");
				echo "\r\n".$this->parseTemplate("rokovnik/meeting_ticket_template.tpl");
			}
		}
		
		//echo $out;
		
	}
	function exportMettingTickets(){
		$ids = explode(";",getFromInput("ids"));
		$out = "";
		foreach ($ids as $id){
			$q = "SELECT DISTINCT d.delatnost_ime, k.komitent_name, rsl.adresa, rsl.kontakt, rsl.kontakt_fja, rp.name as poreklo, rsl.telefon, rsl.ticket_time, rsl.napomena, rsl.komercijalista "
				."FROM rokovnik_sastanak_listici rsl, komitenti k, komitenti_delatnosti kd, delatnosti d, rokovnik_poreklo rp "
				."WHERE k.komitent_id=rsl.komitent "
				."AND k.komitent_id=kd.komdel_kom_id "
				."AND d.delatnost_id=kd.komdel_del_id "
				."AND rp.id=rsl.poreklo "
				."AND rsl.id=$id";
			$res = $this->db->select2($q);
			if ($res !== false) {
				$delatnosti = array();
				foreach ($res as $row){
					if (strlen($row["delatnost_ime"]>25)) {
						$row["delatnost_ime"] = substr($row["delatnost_ime"],0,10)."...".substr($row["delatnost_ime"],strlen($row["delatnost_ime"])-10);
					}
					$delatnosti[] = $row["delatnost_ime"];
				}
				
				$komercijalista_id = $res[0]["komercijalista"];
				if ($komercijalista_id>0) {
					$komercijalista = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$komercijalista_id");
				}else{
					$komercijalista = "nema";
				}
				
				$komitent_name	= $res[0]["komitent_name"];
				$adresa			= $res[0]["adresa"];
				$kontakt		= $res[0]["kontakt"];
				$kontakt_fja	= $res[0]["kontakt_fja"];
				$poreklo		= $res[0]["poreklo"];
				$telefon		= $res[0]["telefon"];
				$time			= $res[0]["ticket_time"];
				$napomena		= $res[0]["napomena"];
				
				$out .= "Komercijalista:\t$komercijalista\r\n";
				$out .= "Vreme sastanka:\t".date($this->getFromConfig("long_date2"),$time)."\r\n";
				$out .= "Naziv komitenta:\t$komitent_name\r\n";
				$out .= "Adresa:\t\t$adresa\r\n";
				$out .= "Tel:\t\t$telefon\r\n";
				$out .= "Delatnosti:\t".implode(", ",$delatnosti)."\r\n";
				$out .= "Kontakt:\t\t$kontakt ($kontakt_fja)\r\n";
				$out .= "Poreklo:\t\t$poreklo\r\n";
				$out .= "Komentar:\t$napomena\r\n";
				$out .= "----------------------------------------------------------------\r\n";
			}
		}
		$tmpfile = "tmp".$this->getUserId();
		$fp = fopen($tmpfile,"w");
		@fwrite($fp,$out);
		fclose($fp);
		echo "$tmpfile{!}sastanci.txt";
	}
	
	function showEdit($id){
		$q = "SELECT rr.time as rrtime, r.komitent_id , k.komitent_name, rs.* "
			."FROM rokovnik r, rokovnik_result rr, rokovnik_sastanak rs, komitenti k "
			."WHERE rs.id=$id "
			."AND rr.id=rs.result_id "
			."AND r.id=rr.rokovnik_id "
			."AND k.komitent_id=r.komitent_id "
			."LIMIT 1";
		list($res) = $this->db->select2($q);
		
		$polja = $this->get_Radnici($res["radnik"]);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		$polja = $this->get_RokovnikFeedbacks($res["fb_id"]);
			$this->assign("fb_id",$polja["polje_id"]);
			$this->assign("fb_desc",$polja["polje_ime"]);
			$this->assign("fb_selected",$polja["polje_selected"]);
		
		$this->assign("result_id",$res["id"]);
		$this->assign("komitent_name",$res["komitent_name"]);
		$this->assign("komitent_id",$res["komitent_id"]);
		
		$this->assign("napomena",$res["napomena"]);
		
		$this->assign("result_id",$res["result_id"]);
		$this->assign("result_time",date($this->getFromConfig("long_date"),$res["rrtime"]));
		$this->assign("result_date",date($this->getFromConfig("short_date"),$res["rrtime"]));
		$this->assign("result_hour",date("H",$res["rrtime"]));
		$this->assign("result_minute",date("i",$res["rrtime"]));
		
		$this->assign("ishod_date",date("d.m.Y",$res["time2"]));
		$this->assign("ishod_hour",date("H",$res["time2"]));
		$this->assign("ishod_minute",date("i",$res["time2"]));
		
		$this->assign("sastanak_id",$id);
		$this->assign("sastanak_time",date($this->getFromConfig("long_date"),$res["time"]));
		$this->assign("sastanak_date",date($this->getFromConfig("short_date"),$res["time"]));
		$this->assign("sastanak_hour",date("H",$res["time"]));
		$this->assign("sastanak_minute",date("i",$res["time"]));
		$this->assign("unet_time",$res["time1"]);
		
		echo $this->parseTemplate("rokovnik/edit_meeting.tpl");
	}	
	function adminCalls(){
		$user = $_COOKIE["ypu"];
		$dan = date("d");
		$mesec = date("m");
		$godina = date("Y");
		$starttime = makeUnixTime(0,0,0,$mesec,$dan,$godina);
				
		$q = "SELECT rokovnik.id, rokovnik.komitent_id, rokovnik.time, komitenti.komitent_name "
			."FROM rokovnik, komitenti "
			."WHERE rokovnik.komitent_id=komitenti.komitent_id "
			."AND rokovnik.user_id=$user AND time>$starttime";
		$res = $this->db->select2($q);
		$num = $this->db->numrows;
		//print_r($res);
		if($num){
			foreach ($res as $k=>$v){
				$ids[]=$v["id"];
				$komids[] = $v["komitent_name"];
				$times[] = date($this->getFromConfig("long_date"),$v["time"]);
			}
			$this->assign("call_id",$ids);
			$this->assign("call_time",$times);
			$this->assign("call_komid",$komids);
		}
		
		echo $this->parseTemplate("rokovnik/admin_calls.tpl");
	}
	function delCall($id){
		$ok = true;
		$q1 = "DELETE FROM rokovnik WHERE id=$id LIMIT 1";
		$q2 = "DELETE FROM rokovnik_result WHERE rokovnik_id=$id LIMIT 1";
		if(!$this->db->alterTable($q1)){
			$ok = false;
		}elseif(!$this->db->alterTable($q2)){
			$ok = false;
		}
		if(!$ok){
			echo mysql_error();
		}else{
			echo "Poziv uspešno obrisan.";
		}
	}
	function editCall($id){
		
		$this->assign("call_id",$id);
		
		list($res) = $this->db->select2("SELECT * FROM rokovnik WHERE id=$id LIMIT 1");
		$komitent = $this->get_RokovnikKomitenti($res["komitent_id"]);
		
		$this->assign("komitent_id",$res["komitent_id"]);
		$this->assign("komitent_name",$komitent["komitent_name"]);
		
		$polja = $this->get_RokovnikPoreklo($res["poreklo"]);
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo_name",$polja["polje_ime"]);
			$this->assign("poreklo_selected",$polja["polje_selected"]);
		$polja = $this->get_RokovnikKontakt($res["kontakt"]);
			$this->assign("kontakt_id",$polja["polje_id"]);
			$this->assign("kontakt_name",$polja["polje_ime"]);
			$this->assign("kontakt_obavljen",$polja["polje_obavljen"]);
			$this->assign("kontakt_selected",$polja["polje_selected"]);
		$polja = $this->get_RokovnikOdgovor($res["odgovor"]);
			$this->assign("odgovor_id",$polja["polje_id"]);
			$this->assign("odgovor_name",$polja["polje_ime"]);
			$this->assign("odgovor_ovlascen",$polja["polje_obavljen"]);
			$this->assign("odgovor_selected",$polja["polje_selected"]);
		
		$this->assign("spec_dogadjaj",$res["napomena_special"]);
		$this->assign("napomena",$res["napomena"]);
		
		list($result) = $this->db->select2("SELECT * FROM rokovnik_result WHERE rokovnik_id=".$res["id"]." LIMIT 1");
		$this->assign("result_id",$result["id"]);
		$polja = $this->get_RokovnikResultTypes($result["type"]);
			$this->assign("result_type_id",$polja["polje_id"]);
			$this->assign("result_type_name",$polja["polje_ime"]);
			$this->assign("result_type_selected",$polja["polje_selected"]);
		
		$this->assign("result_date",date("d.m.Y",$result["time"]));
		$this->assign("result_hour",date("h",$result["time"]));
		$this->assign("result_minute",date("i",$result["time"]));
		$this->assign("result_desc",$result["desc"]);
		
		
		$this->assign("call_time",date($this->getFromConfig("long_date"),$res["time"]));
		
		echo $this->parseTemplate("rokovnik/edit_call.tpl");
	}
	function saveEditCall($post){
		foreach ($post as $k=>$v){
			$post[$k] = trim($v);
		}
		$q = "UPDATE rokovnik SET "
			."";
	}
	function updateAlarms(){
		$day = date("d");
		$mon = date("m");
		$yea = date("Y");
		$start_time = makeUnixTime(0,0,0,$mon,$day,$yea);
		$end_time = makeUnixTime(0,0,0,$mon,$day+1,$yea);
		
		$tipovi_ishoda = array(
			2=>"zovi ponovo",
			5=>"materijali",
			6=>"ponuda",
			7=>"naplata"
		);
		
		$q = "SELECT r.*, rr.time as time1, rr.desc, rrt.name, rk.name as kontakt_name "
			."FROM rokovnik r, rokovnik_result rr, rokovnik_result_type rrt, rokovnik_kontakt rk "
			."WHERE r.id=rr.rokovnik_id "
			."AND rr.type=rrt.id "
			."AND r.kontakt=rk.id "
			."AND r.user_id=".$_COOKIE["ypu"]." "
			."AND (";
			foreach ($tipovi_ishoda as $k=>$v){
				$q .= "rr.type=$k OR ";
			}
		$q = substr($q,0,strlen($q)-4).") ";
		$q .= "AND rr.time>=$start_time AND rr.time<$end_time "
			."ORDER BY rr.time";
		//file_put_contents("debug_rokovnik",$q);
		$res = $this->db->select2($q);
		if($res[0]){
			//echo $q;
			$rest=0;
			foreach ($res as $k=>$row){
				$q = "SELECT komitent_name, komitent_adresa FROM komitenti WHERE komitent_id=".$row["komitent_id"];
				list($komitent_name,$komitent_adresa) = $this->db->getRow($q);
				$adr = $this->adresa->getAll($komitent_adresa);
				$a = '<span title="'.$adr["lokacija"]["mesto"]["ptt"].'">'.$adr["lokacija"]["mesto"]["name"].'</span>';
				if(strlen(trim($adr["ulica"]))){
					$a .= ", ".$adr["ulica"]." ".$adr["broj"];
				}
				$adresa[] = $a;
				
				$q = "SELECT count(id) FROM rokovnik WHERE "
					."komitent_id=".$row["komitent_id"]
					." AND time>".$row["time1"];
				$num = $this->db->getOne($q);
				if($num){
					$called[] = "called";
				}else{
					$rest++;
					$called[] = "";
				}
				$this->assign("called",$called);
				$id[] = $k+1;
				$ishod[] = $row["name"];
				$komitent[] = $komitent_name;
				$call_time[] = date("H:i:s",$row["time1"]);
				$zak_time[] = date($this->getFromConfig("long_date"),$row["time"]);
				$kontakt[] = $row["kontakt_name"];
				$zak_desc[] = $row["desc"];
			}
			
			echo $rest."{!}";
			$this->assign("ishod",$ishod);
			$this->assign("alarm_id",$id);
			$this->assign("komitent",$komitent);
			$this->assign("adresa",$adresa);
			$this->assign("call_time",$call_time);
			$this->assign("zak_time",$zak_time);
			$this->assign("kontakt",$kontakt);
			$this->assign("zak_desc",$zak_desc);
			echo $this->parseTemplate("rokovnik/alarm_list.tpl");
		}else{
			echo 0;
		}
	}
	function updateAlarmsManager(){
		
		$day = date("d");
		$mon = date("m");
		$yea = date("Y");
		
		$timediff = 0;
		
		$start_time = makeUnixTime(0,0,0,$mon,$day,$yea) - $timediff;
		$end_time = makeUnixTime(0,0,0,$mon,$day+1,$yea) - $timediff;
		
		//die("2{!}".date("d.m.Y H:i:s",$start_time)." - $start_time<br />".date("d.m.Y H:i:s",$end_time)." - $end_time");
		
		$tipovi_ishoda = array(
			7=>"zvati kasnije",
			8=>"zainteresovan - odgovor"
		);
		
		$q = "SELECT r.komitent_id, rr.time as time1, rf.name as feedback, rs.napomena, rs.radnik, rs.user  "
			."FROM rokovnik r, rokovnik_result rr, rokovnik_sastanak rs, rokovnik_feedback rf "
			."WHERE r.id=rr.rokovnik_id "
			."AND rr.id=rs.result_id "
			."AND rs.fb_id=rf.id "
			//."AND rs.user=".$_COOKIE["ypu"]." "
			."AND (";
			foreach ($tipovi_ishoda as $k=>$v){
				$q .= "rs.fb_id=$k OR ";
			}
		$q = substr($q,0,strlen($q)-4).") ";
		$q .= "AND rs.time2>=$start_time AND rs.time2<$end_time "
			."ORDER BY rs.time2";
		//file_put_contents("debug_rokovnik",$q);
		$res = $this->db->select2($q);
		
		//die("2{!}".$q);
		
		$out = "";
		if($res[0]){
			//echo $q;
			
			$rest=0;
			foreach ($res as $k=>$row){
				
				// komitent data
				$q = "SELECT komitent_name, komitent_adresa FROM komitenti WHERE komitent_id=".$row["komitent_id"];
				list($komitent_name,$komitent_adresa) = $this->db->getRow($q);
				$adr = $this->adresa->getAll($komitent_adresa);
				$a = '<span title="'.$adr["lokacija"]["mesto"]["ptt"].'">'.$adr["lokacija"]["mesto"]["name"].'</span>';
				if(strlen(trim($adr["ulica"]))){
					$a .= ", ".$adr["ulica"]." ".$adr["broj"];
				}
				$adresa[] = $a;
				
				// radnik data
				$q = "SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$row["radnik"]." LIMIT 1";
				list($radnik_ime,$radnik_prezime) = $this->db->getRow($q);
				
				// user data
				$q = "SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM users, radnici WHERE users.radnik_id=radnici.radnik_id AND users.id=".$row["user"]." LIMIT 1";
				$user_radnik = $this->db->getOne($q);
				
				$id[] = $k+1;
				$komitent[] = $komitent_name;
				$uneo[] = $user_radnik;
				$radnik[] = "$radnik_ime $radnik_prezime";
				$napomena[] = strlen(trim($row["napomena"]))>0?"<img src=\"gui/icons/napomena.gif\" class=\"napomena\" onclick=\"alert(this.nextSibling.value)\" /><textarea>".$row["napomena"]."</textarea>":"";
				$sastanak_time[] = date("d.m.Y H:i:s", $row["time1"]);
				$ishod[] = $row["feedback"];
			}
			
			echo count($res)."{!}";
			$this->assign("alarm_id",$id);
			$this->assign("komitent",$komitent);
			$this->assign("adresa",$adresa);
			$this->assign("uneo",$uneo);
			$this->assign("radnik",$radnik);
			$this->assign("napomena",$napomena);
			$this->assign("sastanak",$sastanak_time);
			$this->assign("ishod",$ishod);
			
			echo $this->parseTemplate("rokovnik/alarm_list_manager.tpl");
			//echo "<li>asss</li>";
		}else{
			echo 0;
		}
	}
	
	function checkSuperPass($pass){
		$superpass = $this->getFromConfig("superpass");
		if (md5($pass)==$superpass) {
			return true;
		}else{
			return false;
		}
	}
	
	//----------------------------------------
	//			GET FUNCTIONS
	//----------------------------------------
	function get_RokovnikPoreklo($id=null){
		$q = "SELECT * FROM rokovnik_poreklo ORDER BY name";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["id"];
			$ret["polje_ime"][$k] = $v["name"];
			if($id == $v["id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikKontakt($id=null){
		$q = "SELECT * FROM rokovnik_kontakt ORDER BY name";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["id"];
			$ret["polje_ime"][$k] = $v["name"];
			$ret["polje_obavljen"][$k] = $v["obavljen"];
			if($id == $v["id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikOdgovor($id=null){
		$q = "SELECT * FROM rokovnik_odgovor WHERE id>0 ORDER BY name";
		$res = $this->db->select2($q);
		if($res[0]){
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_ime"][$k] = $v["name"];
				$ret["polje_ovlascen"][$k] = $v["ovlascen"];
				if($id == $v["id"]){
					$ret["polje_selected"][$k] = " selected";
				}else{
					$ret["polje_selected"][$k] = "";
				}
			}
		}else{
			$ret = array();
		}
		return $ret;
	}
	function get_RokovnikResultTypes($id=null){
		$q = "SELECT * FROM rokovnik_result_type ORDER BY name";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["id"];
			$ret["polje_ime"][$k] = $v["name"];
			if($id == $v["id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikFeedbacks($id=null){
		$q = "SELECT * FROM rokovnik_feedback ORDER BY name";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["id"];
			$ret["polje_ime"][$k] = $v["name"];
			if($id == $v["id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikUgovori($komid){
		require_once("ugovori.fox.class.php");
		$foxugo = new ugovori_fox($komid);
		$ret = $foxugo->get();
		return $ret;
	}
	function get_RokovnikKomitenti($id){
		$q = "SELECT k.komitent_name, k.komitent_adresa, k.komitent_vlasnik_name, k.komitent_direktor, k.komitent_direktor_tel, k.komitent_predst, k.komitent_predst_tel "
			."FROM komitenti k "
			."WHERE k.komitent_id=$id "
			."LIMIT 1";
		list($res) = $this->db->select2($q);
		
		return $res;
	}
	function get_Radnici($id=null,$akt=null){
		$q = "SELECT * FROM radnici ";
		if ($akt != null) {
			$q .= "WHERE radnik_akt='$akt' ";
		}
		$q .= "ORDER BY radnik_ime, radnik_prezime";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["radnik_id"];
			$ret["polje_ime"][$k] = $v["radnik_ime"];
			$ret["polje_prezime"][$k] = $v["radnik_prezime"];
			if($id == $v["radnik_id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikUsers($id=null){
		$q = "SELECT id, radnik_ime, radnik_prezime, username "
			."FROM radnici, users "
			."WHERE users.radnik_id=radnici.radnik_id "
			."AND radnik_akt=1 "
			."AND username!='user' AND username!='root' "
			."ORDER BY radnik_ime, radnik_prezime, username";
		$res = $this->db->select2($q);
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["id"];
			$ret["polje_ime"][$k] = $v["radnik_ime"];
			$ret["polje_prezime"][$k] = $v["radnik_prezime"];
			$ret["polje_username"][$k] = $v["username"];
			if($id == $v["id"]){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_RokovnikTeritorije($id=null,$date=null,$date1=null){
		$q = "SELECT * FROM rokovnik_teritorije";
		if(strlen($date)){
			$q .= " WHERE date='$date'";
		}
		$q .= " ORDER BY date DESC, code ASC";
		$res = $this->db->select2($q);
		$ret = array();
		if($res[0]){
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_code"][$k] = $v["code"];
				$ret["polje_desc"][$k] = $v["desc"];
				$ret["polje_date"][$k] = $v["date"];
				if($id==$v["id"]){
					$ret["polje_selected"][$k] = " selected";
				}else{
					$ret["polje_selected"][$k] = "";
				}
				if($date1 == $v["date"]){
					$ret["polje_display"][$k] = ' display:block;"';
				}else{
					$ret["polje_display"][$k] = ' display:none;"';
				}
			}
		}
		return $ret;
	}
}

?>